﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace Htx.Rbmq.ProducerApp
{
    class Send
    {
        static void Main(string[] args)
        {
            //M1();
            //M2();
            //M3();
            //M4();
            M5();
        }

        /// <summary>
        /// 基础
        /// </summary>
        static void M1()
        {
            var factory = new ConnectionFactory
            { HostName = "81.68.253.181", Port = 5672, UserName = "admin", Password = "000000"/*, VirtualHost = "my_vhost"*/ };
            using var conn = factory.CreateConnection();
            using var channel = conn.CreateModel();
            channel.QueueDeclare(                               // 定义队列
                    queue: "hello",
                    durable: false,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null
                );
            var message = "Hello world！";
            var body = Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(                               // 发布消息
                    exchange: "",
                    routingKey: "hello",
                    basicProperties: null,
                    body: body
                );
            Console.WriteLine("[x] send {0}", message);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();

        }

        /// <summary>
        /// 工作队列
        /// </summary>
        static void M2()
        {
            var factory = new ConnectionFactory
            { HostName = "81.68.253.181", Port = 5672, UserName = "admin", Password = "000000"/*, VirtualHost = "my_vhost"*/ };
            using var conn = factory.CreateConnection();
            using var channel = conn.CreateModel();
            channel.QueueDeclare(
                    queue: "task_queue",
                    durable: true,                                  // 队列持久化
                    exclusive: false,
                    autoDelete: false,
                    arguments: null
                );
            var properties = channel.CreateBasicProperties();
            properties.Persistent = true;                           // 消息持久化
            while (true)
            {
                var message = Console.ReadLine();
                var body = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish(
                        exchange: "",
                        routingKey: "task_queue",
                        basicProperties: properties,                // 加入消息的属性
                        body: body
                    );
                Console.WriteLine("[x] send {0}", message);
            }

        }

        /// <summary>
        /// 发布订阅
        /// </summary>
        private static void M3()
        {
            var factory = new ConnectionFactory
            { HostName = "81.68.253.181", Port = 5672, UserName = "admin", Password = "000000"/*, VirtualHost = "my_vhost"*/ };
            using var conn = factory.CreateConnection();
            using var channel = conn.CreateModel();
            channel.ExchangeDeclare("logs", ExchangeType.Fanout);           // 创建一个日志扇形交换机
            while (true)
            {
                var message = Console.ReadLine();
                channel.BasicPublish(                                       // 向扇形交换机发送消息
                        exchange: "logs",
                        routingKey: "",
                        null,
                        Encoding.UTF8.GetBytes(message)
                    );
            }

        }

        /// <summary>
        /// 路由子集
        /// </summary>
        private static void M4()
        {
            var factory = new ConnectionFactory
            { HostName = "81.68.253.181", Port = 5672, UserName = "admin", Password = "000000"/*, VirtualHost = "my_vhost"*/ };
            using var conn = factory.CreateConnection();
            using var channel = conn.CreateModel();
            channel.ExchangeDeclare("sky", ExchangeType.Direct);                // 创建一个直连交换机
            var list = new List<string> { "red", "black", "orange", "blue" };   // 创建一个颜色列表
            while (true)
            {
                var message = Console.ReadLine();
                var color = list[new Random().Next(0, 4)];
                var msg = message + " | " + color;
                channel.BasicPublish("sky", color, null, Encoding.UTF8.GetBytes(msg));
                Console.WriteLine(msg);
            }

        }

        /// <summary>
        /// 主题交换机
        /// </summary>
        private static void M5()
        {
            var factory = new ConnectionFactory
            { HostName = "81.68.253.181", Port = 5672, UserName = "admin", Password = "000000"/*, VirtualHost = "my_vhost"*/ };
            using var conn = factory.CreateConnection();
            using var channel = conn.CreateModel();
            channel.ExchangeDeclare("animal", ExchangeType.Topic);                  // 创建一个主题交换机
            var animals = new List<string> { "dog", "cat", "monkey" };              // 创建一个动物列表
            var colors = new List<string> { "red", "black", "white" };              // 创建一个颜色列表
            var speeds = new List<string> { "100", "200", "300" };                  // 创建一个速度列表
            while (true)
            {
                var rdm = new Random();
                var routingKey = $"{animals[rdm.Next(0, 3)]}.{colors[rdm.Next(0, 3)]}.{speeds[rdm.Next(0, 3)]}";
                var message = Console.ReadLine();
                var msg = message + $" [{routingKey}]";
                channel.BasicPublish("animal", routingKey, null, Encoding.UTF8.GetBytes(msg));
                Console.WriteLine($"发送的消息：{msg}");
            }

        }

    }
}
